<template>
	<view class="login-container">
		<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <button type="primary" class="btn-login" @click="getUserInfos">一键登录</button>
    <view class="tips-text">
      登录之后尽享权益
    </view>
	</view>
</template>

<script>
  import {mapMutations,mapState} from 'vuex'
	export default {
		data() {
			return {
				
			};
		},
    computed:{
      ...mapState('m_user',['redirectInfo'])
    },
    methods:{
      ...mapMutations('m_user',['updateUserInfo','updateToken','updateRedirectInfo']),
      getUserInfos(e){
        console.log('333',this.redirectInfo)
        uni.getUserProfile({
          desc:'获取用户信息啊啊啊啊',
          success:(res)=>{
            console.log('执行授权：',res.userInfo)
            this.updateUserInfo(res.userInfo)
            
            this.getToken(res)
            
         
          },
          fail:(res)=>{
            return uni.$showMessage('您取消了登录授权')
          }
        })

                
      },
      async getToken(info){
       const [err,res] = await  uni.login().catch(err=>err)
       console.log(res)
       if(err || res.errMsg !=='login:ok') return uni.$showMessage('登陆失败')
       
       const query={
         code:res.code,
         encryptedData: info.encryptedData,
         iv: info.iv,
         rawData: info.rawData,
         signature: info.signature
       }
       // console.log(query)
       
        // const {data:loginResult}  = await uni.$http.post('/api/public/v1/users/wxlogin',query)
        // console.log(loginResult)
        // if(loginResult.meta.status !==200) return uni.$showMessage('登录失败')
        uni.$showMessage('登录成功')
        // this.updateToken(res.code)
        this.updateToken('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo')
        
   // 判断 vuex 中的 redirectInfo 是否为 null
             // 如果不为 null，则登录成功之后，需要重新导航到对应的页面
             this.navigateBack()  
      },
      navigateBack(){
        console.log('redirectInfo1',this.redirectInfo)
        if(this.redirectInfo && this.redirectInfo.openType === 'switchTab'){
          console.log('redirectInfo2',this.redirectInfo)

            // 调用小程序提供的 uni.switchTab() API 进行页面的导航
              uni.switchTab({
                // 要导航到的页面地址
                url: this.redirectInfo.from,
                // 导航成功之后，把 vuex 中的 redirectInfo 对象重置为 null
                complete: () => {
                   console.log('redirectInfo3',this.redirectInfo)
                  // this.updateRedirectInfo(null)
                   console.log('redirectInfo4',this.redirectInfo)
                }
              })
               
        }
      }
      
      
    }
	}
</script>

<style lang="scss">
.login-container{
  height: 750rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #f8f8f8;
  position: relative;
  overflow: hidden;
  &::after{
    content: '';
    display: block;
    width: 100%;
    height: 40px;
    background-color: white;
    position: absolute;
    left: 0;
    bottom: 0;
    border-radius: 100%;
    transform: translateY(50%);
  }
  .btn-login{
    width: 90%;
    background-color: #C00000;
    margin: 15px 0;
    border-radius: 100px;
  }
  .tips-text{
    font-size: 12px;
    color: gray;
  }
}
</style>
